package ListDemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-05-1
 * Time: 21:44
 */
public class MySingList {

    static class NodeList {
        public int value;
        public NodeList next;

        public NodeList(int value) {
            this.value = value;
        }

    }

    public NodeList head;

    public void CreateList() {
        NodeList nodeList = new NodeList(15);
        NodeList nodeList1 = new NodeList(20);
        NodeList nodeList2 = new NodeList(30);
        NodeList nodeList3 = new NodeList(40);
        NodeList nodeList4 = new NodeList(55);


        nodeList.next = nodeList1;
        nodeList1.next = nodeList2;
        nodeList2.next = nodeList3;
        nodeList3.next = nodeList4;

        this.head = nodeList;
    }

    public void Display() {
        NodeList node = this.head;

        while(node != null) {
            System.out.print(node.value+" ");
            node = node.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data) {
        NodeList node = new NodeList(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        NodeList node = new NodeList(data);
        NodeList cur = this.head;

        while(cur.next != null) {
            cur = cur.next;
        }

        cur.next = node;
    }

    private NodeList findIndex(int index) {
        NodeList cur = this.head;

        while(index-1 > 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {


        if(!(index >=0 && index <=size())) {
            throw new IndexErrorExecption("下标错误：");
        }

        if(index == 0) {
            addFirst(data);
            return;
        }

        if (index == size()) {
            addLast(data);
            return;
        }

        NodeList node = new NodeList(data);
        NodeList cur = findIndex(index);
        node.next =cur.next;
        cur.next = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
       NodeList node = this.head;

       while(node != null) {
           if(node.value == key) {
               return true;
           }
           node = node.next;
       }
       return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(this.head == null) {
            System.out.println("链表为空！");
            return;
        }
        if(this.head.value == key) {
            this.head = this.head.next;
            return;
        }

        NodeList node = this.head;
        while(node.next != null) {
            if(node.next.value == key) {
                node.next = node.next.next;
                return;
            }
            node = node.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }

        NodeList node = this.head;
        NodeList cur = head.next;
        while(cur != null) {
            if(cur.value == key) {
                node.next = cur.next;
                cur=cur.next;
            } else {
                node = cur;
                cur = cur.next;
            }
        }
        if(this.head.value == key) {
            this.head = head.next;
        }
    }
    //得到单链表的长度
    public int size() {
        NodeList node = this.head;
        int count = 0;

        while(node != null) {
            count++;
            node = node.next;
        }
        return count;
    }


    public void clear() {
        /*this.head = null;*/
        NodeList node = this.head;
        NodeList cur = this.head;

        while(cur != null) {
            node = cur.next;
            cur.next = null;
            cur = node;
        }

        this.head = null;
    }
}